

from rest_framework.pagination import PageNumberPagination
from rest_framework.response import Response

# 自定义分页器，构造我们业务约定的分页结构数据
class MyPage(PageNumberPagination):
    page_size = 5
    max_page_size = 20
    page_query_param = 'page'
    page_size_query_param = 'pagesize'

    # 重写get_paginated_response来自定义分页结构数据
    def get_paginated_response(self, data):
        # 参数data就是当前页数据 --> 查询集序列化的结果
        return Response({
            'counts': self.page.paginator.count,
            'lists': data,
            'page': self.page.number, # 第几页
            'pages': self.page.paginator.num_pages, # 总页数
            'pagesize': self.page_size # 默认页容量
        })